<link rel="stylesheet" href="/Static/mobile/vant/index.css">
<link rel="stylesheet" href="//at.alicdn.com/t/font_813618_zjhrht6jg9g.css">
<script src="//at.alicdn.com/t/font_813618_zjhrht6jg9g.js"></script>

<script src="/Static/mobile/vant/vue.min.js"></script>
<script src="/Static/mobile/vant/vant.min.js"></script>
<div style="display:none;" id="app">
    <!-- <div style="text-align: center;margin-top:50%;">
        <svg @click="login" class="icon" style="width:60px;height:60px;" aria-hidden="true">
            <use xlink:href="#icon-wechat"></use>
        </svg>
        <br>
        微信登录
    </div> -->
    <div style="padding:0 15%;margin-top:50%;">
         <van-button v-if="haswx" @click="login" round block  type="primary">微信登录</van-button><br> 
       <!--  <van-button @click="mobileLoginShow=true" round block  type="warning">手机登录</van-button>-->
    </div>

    <van-dialog
        v-model="mobileLoginShow"
        show-cancel-button
        :confirm-button-text="activeTab==0?'登录':'注册'"
        :before-close="beforeClose">
        <van-tabs v-model="activeTab">
            <van-tab title="登录">
                <van-field
                    v-model="loginInfo.mobile"
                    type="tel"
                    label="手机号"
                    placeholder="请输入手机号"
                ></van-field>
                <van-field
                    v-model="loginInfo.password"
                    type="password"
                    label="密码"
                    placeholder="请输入密码"
                ></van-field>
            </van-tab>
            <van-tab title="注册">
                <van-field
                    v-model="register.mobile"
                    type="tel"
                    label="手机号"
                    placeholder="请输入手机号"
                ></van-field>
                <van-field
                    v-model="register.code"
                    center
                    clearable
                    label="短信验证码"
                    placeholder="请输入短信验证码"
                >
                    <van-button @click="sendSmsCode" slot="button" size="small" type="primary">{{sendText}}<span v-if="sendNum!=0">{{sendNum}}</span></van-button>
                </van-field>
                <van-field
                    v-model="register.password"
                    type="password"
                    label="密码"
                    placeholder="请输入密码"
                ></van-field>
            </van-tab>
        </van-tabs>
    </van-dialog>
</div>

<script>
    
    function vueInit () {
        var app = new Vue({
            el: '#app',
            data: {
                mobileLoginShow: false,
                loginInfo:{},
                register:{},
                haswx: false,
                activeTab:0,
                sendNum:0,
                sendText:'发送验证码'
            },
            watch: {
                sendNum: function (val,oldval) {
                    if(val!=0) {
                        this.sendText = '已发送';
                    }else{
                        this.sendText = '发送验证码';
                        clearInterval(window.sendTask);
                    }
                }
            },
            created:function () {
                var self = this;
            },
            mounted: function () {
                
                var self = this;
                if(plus.runtime.isApplicationExist({pname:'com.tencent.mm',action:'weixin://'})){
                    self.haswx = true;
                    if(self.haswx) {
                        var token = plus.storage.getItem('token');
                        if(token) {
                            //window.location.replace('/mobile/index?token='+token);
                        }
                    }
                }else{
                }
                
            },
            methods: {
                sendSmsCode: function () {
                    var self = this;
                    if(!this.register.mobile) {
                        this.$toast('请输入手机号');
                        return;
                    }
                    if(this.sendText!='发送验证码') {
                        return false;
                    }
                    var url = "/mobile/login/sendsms?mobile="+this.register.mobile;
                    $.get(url).then(function(res){
                        if(res.ret==1) {
                            showerror(res.msg);
                        }else{
                            self.sendNum = 60;
                            window.sendTask = setInterval(function(){
                                self.sendNum--;
                            },1000)
                        }
                    });
                },
                beforeClose: function(action, done) {
                    if (action === 'confirm'&&this.activeTab==0) {
                        var data = this.loginInfo;
                        if(!data.mobile) {
                            this.$toast('请输入手机号');
                            done(false);
                            return;
                        }
                        if(!data.password) {
                            this.$toast('请输入密码');
                            done(false);
                            return;
                        }
                        $.ajax({
                            type: 'post',
                            data: data,
                            url: '/mobile/login/appmobile'
                        }).then(function(res) {
                            if(res.ret==0) {
                                plus.storage.setItem('token', getCookie('token'));
                                window.location.replace('/mobile/index');
                            }else{
                                done(false);
                                showerror(res.msg);
                            }
                        })
                    }else if(action === 'confirm'&&this.activeTab==1){
                        var data = this.register;
                        if(!data.mobile) {
                            this.$toast('请输入手机号');
                            done(false);
                            return;
                        }
                        if(!data.code) {
                            this.$toast('请输入验证码');
                            done(false);
                            return;
                        }
                        if(!data.password) {
                            this.$toast('请输入密码');
                            done(false);
                            return;
                        }
                        $.ajax({
                            type: 'post',
                            data: data,
                            url: '/mobile/login/register'
                        }).then(function(res) {
                            if(res.ret==0) {
                                window.location.replace('/mobile/index');
                            }else{
                                done(false);
                                showerror(res.msg);
                            }
                        })
                    } else {
                        done();
                    }
                },
                login: function () {
                    var self = this
                    if(!window.plus) {
                        return false
                    }
                    //return false; 
                    plus.oauth.getServices( function (auths) {
                        var wxauth;
                        for(var i=0;i<auths.length;i++) {
                            if(auths[i].id=='weixin') {
                                wxauth = auths[i];
                                break;
                            }
                        }


                        wxauth.authorize(function(e){
                            //alert("授权ok："+e.code);
                            $.ajax({
                                type: 'post',
                                data: {
                                    code:e.code
                                },
                                url: '/mobile/login/app'
                            }).then(function(res) {
                                if(res.ret==0) {
                                    plus.storage.setItem('token', getCookie('token'));
                                    window.location.replace('/mobile/index');
                                }else{
                                    showerror(res.msg);
                                }
                            })
                        }, function(e){
                            alert("授权失败："+JSON.stringify(e));
                        }, {scope:'snsapi_userinfo',state:'authorizetest',appid:'wxb95a3deeed5f0cb0'});
                        

                        /* if(wxauth.authResult){
                            $.ajax({
                                type: 'post',
                                data: wxauth.authResult,
                                url: '/mobile/login/app'
                            }).then(function(res) {
                                if(res.ret==0) {
                                    plus.storage.setItem('token', getCookie('token'));
                                    window.location.replace('/mobile/index');
                                }else{
                                    showerror(res.msg);
                                }
                            })
                        }else{
                            wxauth.login( function(info){
                                $.ajax({
                                    type: 'post',
                                    data: info.target.authResult,
                                    url: '/mobile/login/app'
                                }).then(function(res) {
                                    if(res.ret==0) {
                                        plus.storage.setItem('token', getCookie('token'));
                                        window.location.replace('/mobile/index');
                                    }else{
                                        showerror(res.msg);
                                    }
                                })
                            },function(e){
                            }) 
                        }  */
                    })
                }
            }
        });
    }
    document.addEventListener("plusready",function(){
        vueInit();
    },false);
    
    
</script>
<style>
    .van-cell__title {
        max-width: 90px;
    }

    .van-cell__value {
        text-align: left;
    }
</style>